package com.zrkizzy.seckill.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.zrkizzy.seckill.constant.RabbitMqConst.SECKILL_EXCHANGE;
import static com.zrkizzy.seckill.constant.RabbitMqConst.SECKILL_QUEUE;

/**
 * RabbitMQ配置类
 *
 * @author zhangrongkang
 * @since 2023/4/6
 */
@Configuration
public class RabbitMqConfig {

    /**
     * 声明秒杀队列
     *
     * @return 秒杀队列
     */
    @Bean
    public Queue seckillQueue() {
        return new Queue(SECKILL_QUEUE, true);
    }

    /**
     * 秒杀交换机（主题模式）
     *
     * @return 秒杀交换机
     */
    @Bean
    public TopicExchange seckillExchange() {
        return new TopicExchange(SECKILL_EXCHANGE);
    }

    /**
     * 绑定秒杀队列和秒杀交换机
     *
     * @return 绑定对象
     */
    @Bean
    public Binding bindingSeckillDirect() {
        // 绑定秒杀队列和秒杀交换机，并配置路由
        return BindingBuilder.bind(seckillQueue()).to(seckillExchange()).with("seckill.#");
    }
}
